﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;

using HsTec.Mobile.Green.Model;
using HsTec.Mobile.Green.Util;

namespace HsTec.Mobile.Green.BLL
{
    /// <summary>
    /// 业务逻辑类ReportAnalysisOnline 的摘要说明。
    /// </summary>
    public class ReportAnalysisOnlineBLL
    {
        private readonly HsTec.Mobile.Green.DAL.ReportAnalysisOnlineDAL dal = new HsTec.Mobile.Green.DAL.ReportAnalysisOnlineDAL();
        public ReportAnalysisOnlineBLL()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string RA_ID)
        {
            return dal.Exists(RA_ID);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(HsTec.Mobile.Green.Model.ReportAnalysisOnline model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(HsTec.Mobile.Green.Model.ReportAnalysisOnline model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int Delete(string RA_IDs)
        {

            return dal.Delete(RA_IDs);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public HsTec.Mobile.Green.Model.ReportAnalysisOnline GetModel(int RA_ID)
        {

            return dal.GetModel(RA_ID);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataView GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataView GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<HsTec.Mobile.Green.Model.ReportAnalysisOnline> GetModelList(string strWhere)
        {
            DataView dv = dal.GetList(strWhere);
            return DataTableToList(dv.Table);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<HsTec.Mobile.Green.Model.ReportAnalysisOnline> DataTableToList(DataTable dt)
        {
            List<HsTec.Mobile.Green.Model.ReportAnalysisOnline> modelList = new List<HsTec.Mobile.Green.Model.ReportAnalysisOnline>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                HsTec.Mobile.Green.Model.ReportAnalysisOnline model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new HsTec.Mobile.Green.Model.ReportAnalysisOnline();
                    if (dt.Rows[n]["RA_ID"].ToString() != "")
                    {
                        model.RA_ID = int.Parse(dt.Rows[n]["RA_ID"].ToString());
                    }
                    if (dt.Rows[n]["Area_ID"].ToString() != "")
                    {
                        model.Area_ID = int.Parse(dt.Rows[n]["Area_ID"].ToString());
                    }
                    model.Years = dt.Rows[n]["Years"].ToString();
                    if (dt.Rows[n]["Powers"].ToString() != "")
                    {
                        model.Powers = decimal.Parse(dt.Rows[n]["Powers"].ToString());
                    }
                    if (dt.Rows[n]["Powers_1"].ToString() != "")
                    {
                        model.Powers_1 = decimal.Parse(dt.Rows[n]["Powers_1"].ToString());
                    }
                    if (dt.Rows[n]["Powers_1_1"].ToString() != "")
                    {
                        model.Powers_1_1 = decimal.Parse(dt.Rows[n]["Powers_1_1"].ToString());
                    }
                    if (dt.Rows[n]["Powers_1_2"].ToString() != "")
                    {
                        model.Powers_1_2 = decimal.Parse(dt.Rows[n]["Powers_1_2"].ToString());
                    }
                    if (dt.Rows[n]["Powers_1_2_1"].ToString() != "")
                    {
                        model.Powers_1_2_1 = decimal.Parse(dt.Rows[n]["Powers_1_2_1"].ToString());
                    }
                    if (dt.Rows[n]["Powers_1_3"].ToString() != "")
                    {
                        model.Powers_1_3 = decimal.Parse(dt.Rows[n]["Powers_1_3"].ToString());
                    }
                    if (dt.Rows[n]["Powers_2"].ToString() != "")
                    {
                        model.Powers_2 = decimal.Parse(dt.Rows[n]["Powers_2"].ToString());
                    }
                    if (dt.Rows[n]["Powers_2_1"].ToString() != "")
                    {
                        model.Powers_2_1 = decimal.Parse(dt.Rows[n]["Powers_2_1"].ToString());
                    }
                    if (dt.Rows[n]["Powers_3"].ToString() != "")
                    {
                        model.Powers_3 = decimal.Parse(dt.Rows[n]["Powers_3"].ToString());
                    }
                    if (dt.Rows[n]["Powers_3_1"].ToString() != "")
                    {
                        model.Powers_3_1 = decimal.Parse(dt.Rows[n]["Powers_3_1"].ToString());
                    }
                    if (dt.Rows[n]["Powers_3_1_1"].ToString() != "")
                    {
                        model.Powers_3_1_1 = decimal.Parse(dt.Rows[n]["Powers_3_1_1"].ToString());
                    }
                    if (dt.Rows[n]["Powers_3_1_2"].ToString() != "")
                    {
                        model.Powers_3_1_2 = decimal.Parse(dt.Rows[n]["Powers_3_1_2"].ToString());
                    }
                    if (dt.Rows[n]["Powers_3_1_3"].ToString() != "")
                    {
                        model.Powers_3_1_3 = decimal.Parse(dt.Rows[n]["Powers_3_1_3"].ToString());
                    }
                    if (dt.Rows[n]["Powers_3_2"].ToString() != "")
                    {
                        model.Powers_3_2 = decimal.Parse(dt.Rows[n]["Powers_3_2"].ToString());
                    }
                    if (dt.Rows[n]["Energy"].ToString() != "")
                    {
                        model.Energy = int.Parse(dt.Rows[n]["Energy"].ToString());
                    }
                    if (dt.Rows[n]["Energy_1"].ToString() != "")
                    {
                        model.Energy_1 = int.Parse(dt.Rows[n]["Energy_1"].ToString());
                    }
                    if (dt.Rows[n]["Energy_2"].ToString() != "")
                    {
                        model.Energy_2 = int.Parse(dt.Rows[n]["Energy_2"].ToString());
                    }
                    if (dt.Rows[n]["Energy_3"].ToString() != "")
                    {
                        model.Energy_3 = int.Parse(dt.Rows[n]["Energy_3"].ToString());
                    }
                    if (dt.Rows[n]["Energy_4"].ToString() != "")
                    {
                        model.Energy_4 = int.Parse(dt.Rows[n]["Energy_4"].ToString());
                    }
                    if (dt.Rows[n]["Base"].ToString() != "")
                    {
                        model.Base = int.Parse(dt.Rows[n]["Base"].ToString());
                    }
                    if (dt.Rows[n]["Base_1"].ToString() != "")
                    {
                        model.Base_1 = int.Parse(dt.Rows[n]["Base_1"].ToString());
                    }
                    if (dt.Rows[n]["Base_2"].ToString() != "")
                    {
                        model.Base_2 = int.Parse(dt.Rows[n]["Base_2"].ToString());
                    }
                    if (dt.Rows[n]["Base_3"].ToString() != "")
                    {
                        model.Base_3 = int.Parse(dt.Rows[n]["Base_3"].ToString());
                    }
                    if (dt.Rows[n]["Base_4"].ToString() != "")
                    {
                        model.Base_4 = int.Parse(dt.Rows[n]["Base_4"].ToString());
                    }
                    if (dt.Rows[n]["Base_5"].ToString() != "")
                    {
                        model.Base_5 = int.Parse(dt.Rows[n]["Base_5"].ToString());
                    }
                    if (dt.Rows[n]["Base_6"].ToString() != "")
                    {
                        model.Base_6 = int.Parse(dt.Rows[n]["Base_6"].ToString());
                    }
                    if (dt.Rows[n]["Base_7"].ToString() != "")
                    {
                        model.Base_7 = int.Parse(dt.Rows[n]["Base_7"].ToString());
                    }
                    if (dt.Rows[n]["Base_8"].ToString() != "")
                    {
                        model.Base_8 = int.Parse(dt.Rows[n]["Base_8"].ToString());
                    }
                    if (dt.Rows[n]["Base_9"].ToString() != "")
                    {
                        model.Base_9 = int.Parse(dt.Rows[n]["Base_9"].ToString());
                    }
                    if (dt.Rows[n]["Base_10"].ToString() != "")
                    {
                        model.Base_10 = int.Parse(dt.Rows[n]["Base_10"].ToString());
                    }
                    if (dt.Rows[n]["Outdoor"].ToString() != "")
                    {
                        model.Outdoor = int.Parse(dt.Rows[n]["Outdoor"].ToString());
                    }
                    if (dt.Rows[n]["Intelligent"].ToString() != "")
                    {
                        model.Intelligent = int.Parse(dt.Rows[n]["Intelligent"].ToString());
                    }
                    if (dt.Rows[n]["Intelligent_1"].ToString() != "")
                    {
                        model.Intelligent_1 = int.Parse(dt.Rows[n]["Intelligent_1"].ToString());
                    }
                    if (dt.Rows[n]["Intelligent_2"].ToString() != "")
                    {
                        model.Intelligent_2 = int.Parse(dt.Rows[n]["Intelligent_2"].ToString());
                    }
                    model.Intelligent_3 = dt.Rows[n]["Intelligent_3"].ToString();
                    if (dt.Rows[n]["Switch"].ToString() != "")
                    {
                        model.Switch = int.Parse(dt.Rows[n]["Switch"].ToString());
                    }
                    if (dt.Rows[n]["Batteries"].ToString() != "")
                    {
                        model.Batteries = int.Parse(dt.Rows[n]["Batteries"].ToString());
                    }
                    if (dt.Rows[n]["NewBase"].ToString() != "")
                    {
                        model.NewBase = int.Parse(dt.Rows[n]["NewBase"].ToString());
                    }
                    if (dt.Rows[n]["NewBase_1"].ToString() != "")
                    {
                        model.NewBase_1 = int.Parse(dt.Rows[n]["NewBase_1"].ToString());
                    }
                    if (dt.Rows[n]["NewBase_2"].ToString() != "")
                    {
                        model.NewBase_2 = int.Parse(dt.Rows[n]["NewBase_2"].ToString());
                    }
                    if (dt.Rows[n]["NewBase_3"].ToString() != "")
                    {
                        model.NewBase_3 = int.Parse(dt.Rows[n]["NewBase_3"].ToString());
                    }
                    if (dt.Rows[n]["NewBase_4"].ToString() != "")
                    {
                        model.NewBase_4 = int.Parse(dt.Rows[n]["NewBase_4"].ToString());
                    }
                    if (dt.Rows[n]["NewBase_5"].ToString() != "")
                    {
                        model.NewBase_5 = int.Parse(dt.Rows[n]["NewBase_5"].ToString());
                    }
                    if (dt.Rows[n]["NewBase_6"].ToString() != "")
                    {
                        model.NewBase_6 = int.Parse(dt.Rows[n]["NewBase_6"].ToString());
                    }
                    if (dt.Rows[n]["NewBase_7"].ToString() != "")
                    {
                        model.NewBase_7 = int.Parse(dt.Rows[n]["NewBase_7"].ToString());
                    }
                    if (dt.Rows[n]["Recovery"].ToString() != "")
                    {
                        model.Recovery = int.Parse(dt.Rows[n]["Recovery"].ToString());
                    }
                    if (dt.Rows[n]["Recovery_1"].ToString() != "")
                    {
                        model.Recovery_1 = int.Parse(dt.Rows[n]["Recovery_1"].ToString());
                    }
                    if (dt.Rows[n]["Recovery_2"].ToString() != "")
                    {
                        model.Recovery_2 = int.Parse(dt.Rows[n]["Recovery_2"].ToString());
                    }
                    if (dt.Rows[n]["Recovery_3"].ToString() != "")
                    {
                        model.Recovery_3 = int.Parse(dt.Rows[n]["Recovery_3"].ToString());
                    }
                    if (dt.Rows[n]["Recovery_4"].ToString() != "")
                    {
                        model.Recovery_4 = int.Parse(dt.Rows[n]["Recovery_4"].ToString());
                    }
                    if (dt.Rows[n]["NewBatteries"].ToString() != "")
                    {
                        model.NewBatteries = int.Parse(dt.Rows[n]["NewBatteries"].ToString());
                    }
                    if (dt.Rows[n]["NewBatteries_1"].ToString() != "")
                    {
                        model.NewBatteries_1 = int.Parse(dt.Rows[n]["NewBatteries_1"].ToString());
                    }
                    if (dt.Rows[n]["NewBatteries_2"].ToString() != "")
                    {
                        model.NewBatteries_2 = int.Parse(dt.Rows[n]["NewBatteries_2"].ToString());
                    }
                    if (dt.Rows[n]["NewBatteries_3"].ToString() != "")
                    {
                        model.NewBatteries_3 = int.Parse(dt.Rows[n]["NewBatteries_3"].ToString());
                    }
                    if (dt.Rows[n]["Green"].ToString() != "")
                    {
                        model.Green = int.Parse(dt.Rows[n]["Green"].ToString());
                    }
                    if (dt.Rows[n]["Green_1"].ToString() != "")
                    {
                        model.Green_1 = int.Parse(dt.Rows[n]["Green_1"].ToString());
                    }
                    if (dt.Rows[n]["Green_1_1"].ToString() != "")
                    {
                        model.Green_1_1 = int.Parse(dt.Rows[n]["Green_1_1"].ToString());
                    }
                    if (dt.Rows[n]["Green_2"].ToString() != "")
                    {
                        model.Green_2 = int.Parse(dt.Rows[n]["Green_2"].ToString());
                    }
                    if (dt.Rows[n]["SIM"].ToString() != "")
                    {
                        model.SIM = int.Parse(dt.Rows[n]["SIM"].ToString());
                    }
                    if (dt.Rows[n]["SIM_1"].ToString() != "")
                    {
                        model.SIM_1 = int.Parse(dt.Rows[n]["SIM_1"].ToString());
                    }
                    if (dt.Rows[n]["SIM_2"].ToString() != "")
                    {
                        model.SIM_2 = int.Parse(dt.Rows[n]["SIM_2"].ToString());
                    }
                    if (dt.Rows[n]["Lighting"].ToString() != "")
                    {
                        model.Lighting = int.Parse(dt.Rows[n]["Lighting"].ToString());
                    }
                    if (dt.Rows[n]["Lighting_1"].ToString() != "")
                    {
                        model.Lighting_1 = int.Parse(dt.Rows[n]["Lighting_1"].ToString());
                    }
                    if (dt.Rows[n]["Lighting_2"].ToString() != "")
                    {
                        model.Lighting_2 = int.Parse(dt.Rows[n]["Lighting_2"].ToString());
                    }
                    model.Lighting_3 = dt.Rows[n]["Lighting_3"].ToString();
                    if (dt.Rows[n]["Lighting_4"].ToString() != "")
                    {
                        model.Lighting_4 = int.Parse(dt.Rows[n]["Lighting_4"].ToString());
                    }
                    if (dt.Rows[n]["Lighting_5"].ToString() != "")
                    {
                        model.Lighting_5 = int.Parse(dt.Rows[n]["Lighting_5"].ToString());
                    }
                    if (dt.Rows[n]["Other"].ToString() != "")
                    {
                        model.Other = int.Parse(dt.Rows[n]["Other"].ToString());
                    }
                    if (dt.Rows[n]["Other_1"].ToString() != "")
                    {
                        model.Other_1 = int.Parse(dt.Rows[n]["Other_1"].ToString());
                    }
                    if (dt.Rows[n]["Other_2"].ToString() != "")
                    {
                        model.Other_2 = decimal.Parse(dt.Rows[n]["Other_2"].ToString());
                    }
                    if (dt.Rows[n]["Other_3"].ToString() != "")
                    {
                        model.Other_3 = int.Parse(dt.Rows[n]["Other_3"].ToString());
                    }
                    if (dt.Rows[n]["Funds"].ToString() != "")
                    {
                        model.Funds = decimal.Parse(dt.Rows[n]["Funds"].ToString());
                    }
                    if (dt.Rows[n]["Funds_1"].ToString() != "")
                    {
                        model.Funds_1 = decimal.Parse(dt.Rows[n]["Funds_1"].ToString());
                    }
                    if (dt.Rows[n]["Funds_2"].ToString() != "")
                    {
                        model.Funds_2 = decimal.Parse(dt.Rows[n]["Funds_2"].ToString());
                    }
                    if (dt.Rows[n]["Funds_3"].ToString() != "")
                    {
                        model.Funds_3 = decimal.Parse(dt.Rows[n]["Funds_3"].ToString());
                    }
                    if (dt.Rows[n]["Harmonic"].ToString() != "")
                    {
                        model.Harmonic = int.Parse(dt.Rows[n]["Harmonic"].ToString());
                    }
                    if (dt.Rows[n]["AddDate"].ToString() != "")
                    {
                        model.AddDate = DateTime.Parse(dt.Rows[n]["AddDate"].ToString());
                    }
                    if (dt.Rows[n]["OperatUser"].ToString() != "")
                    {
                        model.OperatUser = int.Parse(dt.Rows[n]["OperatUser"].ToString());
                    }
                    if (dt.Rows[n]["OperatDate"].ToString() != "")
                    {
                        model.OperatDate = DateTime.Parse(dt.Rows[n]["OperatDate"].ToString());
                    }
                    model.Remark = dt.Rows[n]["Remark"].ToString();
                    model.AuditState = dt.Rows[n]["AuditState"].ToString();
                    if (dt.Rows[n]["AuditUser"].ToString() != "")
                    {
                        model.AuditUser = int.Parse(dt.Rows[n]["AuditUser"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataView GetAllList()
        {
            return GetList("");
        }

        #endregion  成员方法

        /// <summary>
        /// 批量审核
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int Aduit(string ids)
        {
            return dal.Aduit(ids);
        }

        public List<string> getTableField(string tableName)
        {
            return dal.getTableField(tableName);
        }

        public DataView GetAreaList()
        {
            return SQLHelper.ExecuteDataView("select * from Area where ParentID=10");
        }

        /// <summary>
        /// 弃审
        /// </summary>
        /// <param name="sID">上报数据表ID</param>
        /// <returns></returns>
        public int UnAduit(string sID, string remark)
        {
            return SQLHelper.ExecuteNonQuery("update tb_ReportAnalysisOnline set AuditState='1',OperatDate='" + DateTime.Now + "',Remark='" + remark + "' where RA_ID='" + sID + "'");
        }

        public DataView GetAreaListByPers(string areaid)
        {
            return SQLHelper.ExecuteDataView("select * from Area where ParentID=10 and AreaID='" + areaid + "'");
        }

        public DataView GetAreaList(string sAreaId)
        {
            if (!String.IsNullOrEmpty(sAreaId) && sAreaId != "0")
                return SQLHelper.ExecuteDataView("select * from Area where ParentID=10 and ID='" + sAreaId + "'");
            else
                return GetAreaList();
        }

        public int AddModel(int Area_ID, string Years, DateTime AddDate, int OperatUser, DateTime OperatDate, string AuditState, string Remark, string sList)
        {
            string[] ary = sList.Split(',');
            List<string> lFields = new List<string>();
            List<string> lValues = new List<string>();
            for (int i = 0; i < ary.Length - 1; i++)
            {
                string[] ary1 = ary[i].Split('|');
                lValues.Add(ary1[0]);
                lFields.Add(ary1[1]);
            }
            //生成SQL语句串
            StringBuilder sb = new StringBuilder();
            sb.Append("insert into tb_ReportAnalysisOnline(Area_ID,Years,AddDate,OperatUser,OperatDate,Remark,AuditState,AuditUser");
            foreach (string tmp in lFields)
            {
                sb.Append("," + tmp);
            }
            sb.Append(")values('" + Area_ID.ToString() + "','" + Years + "','" + AddDate.ToString() + "','" + OperatUser.ToString() + "','" + OperatDate.ToString() + "','" + Remark + "','" + AuditState + "','0'");
            foreach (string tmp in lValues)
            {
                sb.Append(",'" + tmp + "'");
            }
            sb.Append(")");
            return SQLHelper.ExecuteNonQuery(sb.ToString());
        }

        public int EditModel(int Area_ID, string Years, DateTime AddDate, int OperatUser, DateTime OperatDate, string AuditState, string Remark, string sList, string sID)
        {
            string[] ary = sList.Split(',');
            List<string> lFields = new List<string>();
            List<string> lValues = new List<string>();
            for (int i = 0; i < ary.Length - 2; i++)
            {
                string[] ary1 = ary[i].Split('|');
                lValues.Add(ary1[0]);
                lFields.Add(ary1[1]);
            }
            //生成SQL语句串
            StringBuilder sb = new StringBuilder();
            sb.Append("update tb_ReportAnalysisOnline set Area_ID='" + Area_ID.ToString() + "'" +
                ",Years='" + Years + "',AddDate='" + AddDate.ToString() + "'" +
                ",OperatUser='" + OperatUser.ToString() + "',OperatDate='" + OperatDate.ToString() + "'" +
                ",Remark='" + Remark + "',AuditState='" + AuditState + "',AuditUser='0'");
            for (int i = 0; i < lFields.Count; i++)
            {
                sb.Append("," + lFields[i] + "='" + lValues[i] + "'");
            }
            sb.Append(" where RA_ID='" + sID + "'");
            return SQLHelper.ExecuteNonQuery(sb.ToString());
        }
    }
}

